﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using System.Linq;
using Manager.App_Code.DTO;
using Manager.App_Code.DAL;
using Manager.App_Code.IDAL;
using Manager.Common;

namespace Manager.FormAdd_Edit
{
    public partial class Frm_SapXepPhong : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        ChiTiet_ChiTietPhieuDatPhong chiTiet_ChiTietPhieuDat = new ChiTiet_ChiTietPhieuDatPhong();
        string tenLoaiPhong = "";
        double donGia = 0;
        string tenKhach = "";

        public Frm_SapXepPhong()
        {
            InitializeComponent();
        }

        public Frm_SapXepPhong(ChiTiet_ChiTietPhieuDatPhong _chitietphieudat, string _tenLoaiPhong, double _donGia, string _tenKhach)
        {
            InitializeComponent();
            chiTiet_ChiTietPhieuDat = _chitietphieudat;
            tenLoaiPhong = _tenLoaiPhong;
            donGia = _donGia;
            tenKhach = _tenKhach;
        }

        private void Frm_SapXepPhong_Load(object sender, EventArgs e)
        {
            if (chiTiet_ChiTietPhieuDat != null)
            {
                var chiTietPhieuDat = db.ChiTietPhieuDatPhongs.Single(i => i.ChiTietPhieuDatPhongId == chiTiet_ChiTietPhieuDat.ChiTietPhieuDatPhongId);
                var lstPhong = db.Phongs.Where(i => i.LoaiPhong == chiTietPhieuDat.MaLoaiPhong && i.TinhTrang == ((int)CommonEnum.TinhTrangPhong.SanSang_1).ToString()).ToList();
                if (lstPhong.Count > 0)
                {
                    gridControl1.DataSource = lstPhong;
                    txtMaPhieuDat.Text = chiTietPhieuDat.MaPhieuDat;
                    txtLoaiPhong.Text = tenLoaiPhong;
                    txtDonGia.Text = donGia.ToString();
                    txtMaPhong.Text = chiTiet_ChiTietPhieuDat.MaPhong;
                    txtSoPhong.Text = chiTiet_ChiTietPhieuDat.MaPhong;
                    memoKhachHang.Text = tenKhach;
                }
            }
        }

        private void butCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaPhong.Text != "")
                {
                    var temp = db.ChiTiet_ChiTietPhieuDatPhongs.Single(i => i.Id == chiTiet_ChiTietPhieuDat.Id);
                    if (!string.IsNullOrEmpty(temp.MaPhong))
                    {
                        var phongTemp = db.Phongs.Single(i => i.MaPhong == temp.MaPhong);
                        phongTemp.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SanSang_1).ToString();
                        db.SubmitChanges();
                    }
                    temp.MaPhong = txtMaPhong.Text;
                    temp.TrangThai = true;
                    var phong = db.Phongs.Single(i => i.MaPhong == temp.MaPhong);
                    phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.ChoKhach_5).ToString();
                    db.SubmitChanges();
                    this.Close();
                }             
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_SapXepPhong:butCapNhat_Click " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            int index = gridView1.FocusedRowHandle;
            txtSoPhong.Text = gridView1.GetRowCellValue(index, TenPhong).ToString();
            txtMaPhong.Text = gridView1.GetRowCellValue(index, MaPhong).ToString();
        }
    }
}